from threading import Thread

from app.exts import influxdb
from app.services.business.gaolu_diagnose.ind.abstract_ind import RealTimeInfluxDbInd
from app.services.chart.Line import Line
from app.utils import date_util
from app.utils.thread import CountDownLatch
from datetime import datetime, timedelta
import logging as log


class LugangBodyC(Line):
    def generate(self, start, end):
        start, end = date_util.parse_start_end_time(start, end)

        class SubThread(Thread):
            def __init__(self, name, latch, params):
                Thread.__init__(self)
                self.name = name
                self.latch = latch
                self.params = params
                self.result = 0

            def run(self):
                name = self.name
                try:
                    influxdb_client = influxdb.get_client()
                    params = self.params

                    instance: RealTimeInfluxDbInd = RealTimeInfluxDbInd(influxdb_client, params)
                    value = instance.get_result()
                    self.result = value
                except Exception as e:
                    log.error("fail to get result of {}. {}".format(name, e), exc_info=1)
                self.latch.countDown()

        data_matrix = {
            "29835": ['CG_LT_GL_GL04_29835WD000090DB1', 'CG_LT_GL_GL04_29835WD000090DB1',
                      'CG_LT_GL_GL04_29835WD090180DN1',
                      'CG_LT_GL_GL04_29835WD090180DN2', 'CG_LT_GL_GL04_29835WD180270XN1',
                      'CG_LT_GL_GL04_29835WD180270XN2',
                      'CG_LT_GL_GL04_29835WD000270XB1', 'CG_LT_GL_GL04_29835WD000270XB3'],
            "32595": ['CG_LT_GL_GL04_32595WD000090DB1', 'CG_LT_GL_GL04_32595WD000090DB1',
                      'CG_LT_GL_GL04_32595WD090180DN1',
                      'CG_LT_GL_GL04_32595WD090180DN2', 'CG_LT_GL_GL04_32595WD180270XN1',
                      'CG_LT_GL_GL04_32595WD180270XN2',
                      'CG_LT_GL_GL04_32595WD000270XB1', 'CG_LT_GL_GL04_32595WD000270XB3'],
            "34800": ['CG_LT_GL_GL04_34800WD000090DB1', 'CG_LT_GL_GL04_34800WD000090DB2',
                      'CG_LT_GL_GL04_34800WD090180DN1',
                      'CG_LT_GL_GL04_34800WD090180DN1', 'CG_LT_GL_GL04_34800WD180270XN1',
                      'CG_LT_GL_GL04_34800WD180270XN2',
                      'CG_LT_GL_GL04_34800WD000270XB1', 'CG_LT_GL_GL04_34800WD000270XB3'],
        }

        latch = CountDownLatch(3 * 8)
        thread_list = []
        for key, value in data_matrix.items():
            for v in value:
                dt_start_tz = date_util.dt_to_str(
                    start - timedelta(hours=8),
                    '%Y-%m-%dT%H:%M:%SZ')
                dt_end_tz = date_util.dt_to_str(
                    end - timedelta(hours=8),
                    '%Y-%m-%dT%H:%M:%SZ')
                params = dict(ind_name=v, dt_start_tz=dt_start_tz, dt_end_tz=dt_end_tz)
                thread = SubThread(v, latch, params)
                thread.start()
                thread_list.append(thread)
        latch.await()

        result = {thread.name: thread.result for thread in thread_list}
        value_matrix = {}
        for key, value in data_matrix.items():
            value_matrix[key] = [round(result[v], 2) for v in value]

        data = dict(name="", y=[], type='radar', tooltip={
            "trigger": 'item'
        })

        for key, value in value_matrix.items():
            data['y'].append(dict(name=key, value=value))

        return dict(
            y_array=[data],
            tooltip={
                "trigger": 'axis'
            },
            desc="炉身状态监测",

            radar={
                "indicator": [
                    {"name": '0'},
                    {"name": '45'},
                    {"name": '90'},
                    {"name": '135'},
                    {"name": '180'},
                    {"name": '225'},
                    {"name": '270'},
                    {"name": '315'}],
            }, legend={
                "data": value_matrix.keys()
            })
